/**
 * 
 */
package com.yidu.menuProject.action;

import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.menuProject.commont.IndustrySMS;
import com.yidu.menuProject.commont.Tools;
import com.yidu.menuProject.model.FoodModel;
import com.yidu.menuProject.model.KeepModel;
import com.yidu.menuProject.model.MealModel;
import com.yidu.menuProject.model.MealtypeModel;
import com.yidu.menuProject.model.TuserModel;
import com.yidu.menuProject.service.MealServiceHWT;


/**
 * @author Bruce
 * @date 2018年6月9日
 * @version 1.0
 */
@Controller
@RequestMapping("/meal")
public class MealActionHwt {
	//客户服务接口
	@Resource
	private MealServiceHWT mealService ; 
	/**
	 * 查询出所有的店家
	 */
	@RequestMapping("/selectFindAll")
	@ResponseBody 	//将返回的数据转换为json
	public Object selectFindAll(String da ,String mealName){
		if(mealName==null || mealName =="") {
			List<MealModel> list = mealService.selectFindAll(da);
			return list;
		}else {
			List<MealModel> list = mealService.selectFindAllVague(mealName);
			return list;
		}
	}

	//获取验证码
	/**
	 * 短信验证
	 */
	@RequestMapping("/registYZM")
	@ResponseBody 	//将返回的数据转换为json
	public Object registYZM(String userPhone ,HttpServletRequest request){
		System.out.println("电话号码"+userPhone);
		//先定义一个空的String
		String str = "";
		//定义随机
		Random rd=new Random();
		//循环取4个随机数
		for(int i=0;i<4;i++){
			int k=rd.nextInt(9);
			str+=k;
		}
		//使用色是sesstion保存以便注册时判断短信验证码是否填写正确
		request.getSession().setAttribute("code", str);
		//短信内容
		String con = "【馋了科技】您的验证码为"+str+"，请于5分钟内正确输入，如非本人操作，请忽略此短信。";
		//验证码通知短信接口
		IndustrySMS.execute(con, userPhone);
		return str;
	}

	/**
	 * 用户注册
	 */
	@RequestMapping("/registerUser")
	public Object registerUser(TuserModel tm,String yzm ,HttpServletRequest request){
		String str = (String) request.getSession().getAttribute("code");
		if(yzm.equals(str)) {
			int num = mealService.insertSelectiveUser(tm);
			if(num>0) {
				return "login";
			}else {
				return "register";
			}
		}else {
			return "register";
		}
	}
	
	/**
	 * 用户登录
	 */
	@RequestMapping("/loginUser")
	public Object loginUser(TuserModel tm,HttpServletRequest request){
		TuserModel tuserModel = mealService.loginUser(tm);
		if(tuserModel!=null){
			request.getSession().setAttribute("tuserModel", tuserModel);
			return "redirect:/pages/index.html";
		}else{
			return "login";
		}
	}
	/**
	 * 判断用户是否用户登录进入个人中心
	 */
	@RequestMapping("/loginUserGRZX")
	public Object loginUserGRZX(HttpServletRequest request){
		TuserModel tuserModel =(TuserModel) request.getSession().getAttribute("tuserModel");
		if(tuserModel!=null){
			return "redirect:/pages/html/member_order.html";
		}else{
			return "redirect:/pages/login.html";
		}
	}
	/**
	 * 查询出所有的店铺类型
	 */
	@RequestMapping("/findAllType")
	@ResponseBody 	//将返回的数据转换为json
	public Object findAllType(){
		List<MealtypeModel> list = mealService.findAllType();
		return list;
	}
	/**
	 * 跳转到店铺详情
	 */
	@RequestMapping("/details")
	public Object details(String mealId,HttpServletRequest request){
		request.getSession().setAttribute("mealId", mealId);
		return "showFood2";
	}
	
	/**
	 * 详情页面数据传递
	 */
	@RequestMapping("/findMeal")
	@ResponseBody 	//将返回的数据转换为json
	public Object findMeal(HttpServletRequest request){
		String mealId = (String) request.getSession().getAttribute("mealId");
		MealModel meal =  mealService.selectByPrimaryKey(mealId);
		request.getSession().removeAttribute("mealId");
		
		return meal;
	}
	
	/**
	 * 详情菜品数据传递
	 */
	@RequestMapping("/detailedMeal")
	@ResponseBody 	//将返回的数据转换为json
	public Object detailedMeal(String mealId,HttpServletRequest request){
		List<FoodModel> list  =  mealService.findAllFood(mealId);
		return list;
	}
	/**
	 * 增加购物车
	 */
	@RequestMapping("/addKeep")
	@ResponseBody 	//将返回的数据转换为json
	public Object addKeepHWT(String id,double price, HttpServletRequest request){
		KeepModel keepModel = new KeepModel();
		keepModel.setKeepPrice(price);
		keepModel.setFoodId(id);
		keepModel.setSort(1);
		String kId = Tools.getRandomString(16);
		keepModel.setKeepId(kId);
		TuserModel tm = (TuserModel) request.getSession().getAttribute("tuserModel");
		keepModel.setUserId(tm.getUserId());
		mealService.addKeepHWT(keepModel);
		
		return kId;
	}
	/**
	 * 修改购物车数量
	 */
	@RequestMapping("/updateKeep")
	@ResponseBody 	//将返回的数据转换为json
	public Object updateKeepHWT(int sort,String id){
		KeepModel keepModel = new KeepModel();
		keepModel.setSort(sort);
		keepModel.setKeepId(id);
		mealService.updateKeepHWT(keepModel);
		
		return "修改成功";
	}
	/**
	 * 跳转到结算
	 */
	@RequestMapping("/jsForm")
	public Object jsForm(String gucSJ, HttpServletRequest request){
		TuserModel tuserModel = (TuserModel) request.getSession().getAttribute("tuserModel");
		if(tuserModel==null) {
			return "redirect:/pages/login.html";
		}else {
			request.getSession().setAttribute("gucSJ", gucSJ);
			return "redirect:/pages/other/account.html";
		}
	}
}
